package com.cws.business.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cws.business.IActitviyBusiness;
import com.cws.model.vo.ProcessDefinitionVO;
import com.cws.model.vo.TaskVO;
import com.cws.service.BaseService;

@Service
@Transactional
public class ActitviyBusinessImpl extends BaseService  implements IActitviyBusiness {

	
	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	ProcessEngine processEngine;
	
	
	
	
	
	
	@Override
	public List<ProcessDefinitionVO> getAllProcessDefinition() {
		List<ProcessDefinitionVO> result = new ArrayList<ProcessDefinitionVO>();
		List<ProcessDefinition> definitions = repositoryService.createProcessDefinitionQuery().list();
		if(null != definitions && !definitions.isEmpty()){
			for (ProcessDefinition processDefinition : definitions) {
				result.add(new ProcessDefinitionVO(processDefinition.getKey(), processDefinition.getDeploymentId(), processDefinition.getName()));
			}
		}
		return result;
	}

	@Override
	public List<Deployment> getAllDeployment() {
		List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
		return deployments;
	}
	
	@Override
	public Deployment deploy(String businessKey) {
		Deployment deployment = repositoryService.createDeployment().addClasspathResource("/diagrams/"+businessKey+".bpmn").deploy();
		return deployment;
	}

	
	
	@Override
	public List<Task> getAllTasks() {
		List<Task> tasks = taskService.createTaskQuery().list();
		return tasks;
	}
	
	@Override
	public List<TaskVO> getAllTaskList() {
		 List<TaskVO> taskVOs = new ArrayList<TaskVO>();
		List<Task> tasks = taskService.createTaskQuery().list();
		if(!tasks.isEmpty()){
			for (Task task : tasks) {
				TaskVO taskVO = new TaskVO(task.getId(), task.getName(), task.getAssignee());
				taskVOs.add(taskVO);
			}
		}
		
		return taskVOs;
	}

	@Override
	public ProcessInstance startProcess(String processDefinitionKey,Map<String, Object> variables) {
		ProcessInstance processInstance =  runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
		return processInstance;
	}

	@Override
	public ProcessInstance startProcess(String processDefinitionKey) {
		ProcessInstance instance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
		return instance;
	}

	@Override
	public List<ProcessInstance> getAllStartProcess() {
		List<ProcessInstance> instances = runtimeService.createProcessInstanceQuery().list();
		return instances;
	}

	@Override
	public List<TaskVO> getAllTaskListByAssignee(String assignee) {
			List<TaskVO> taskVOs = new ArrayList<TaskVO>();
			List<Task> tasks = taskService.createTaskQuery().taskAssigneeLike(assignee).list();
			if(!tasks.isEmpty()){
				TaskVO taskVO = null;
				for (Task task : tasks) {
					taskVO = new TaskVO(task.getId(), task.getName(), task.getAssignee());
					taskVOs.add(taskVO);
				}
			}
			return taskVOs;
	}
	 
	@Override
	public boolean  claimTask(String taskId,String userId){
		try { 
			taskService.claim(taskId, userId);
			return true;
		} catch (Exception e) {
			
			
		}
		return false;
	}


}
